package com.all.play.myapplication;

import android.os.Bundle;
import android.support.design.widget.BaseTransientBottomBar;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private TextInputLayout inputLayout;
    private TextInputEditText inputEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inputLayout = (TextInputLayout) findViewById(R.id.til_p);
        inputEditText = (TextInputEditText) findViewById(R.id.til_c);

        EditText editText = inputLayout.getEditText();
//        editText.addTextChangedListener();



        //开始计数
        inputLayout.setCounterEnabled(true);
        inputLayout.setCounterMaxLength(6);


        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (inputLayout.getEditText().getText().toString().length()>6){
                    inputLayout.setErrorEnabled(true);
                    inputLayout.setError("超出长度限制");

                }else{
                    inputLayout.setErrorEnabled(false);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });





    }

    public void showSnackbar(View view){


        Snackbar snackbar = Snackbar.make(view, "确定", 3000);
        snackbar.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
            @Override
            public void onDismissed(Snackbar transientBottomBar, int event) {
                super.onDismissed(transientBottomBar, event);
            }

            @Override
            public void onShown(Snackbar transientBottomBar) {
                super.onShown(transientBottomBar);
            }
        });
        snackbar.show();
    }

}
